package com.lkd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lkd.dto.UserDto;
import com.lkd.entity.Role;
import com.lkd.entity.User;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.RoleService;
import com.lkd.service.UserService;
import com.lkd.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    //角色列表
    @GetMapping("/api/user-service/role")
    public Result findList() {
        List<Role> list = roleService.findList();
        return Result.success(list);
    }

    //新增人员
    @PostMapping("/api/user-service/user")
    public Result save(@RequestBody UserDto userDto) {
        //此方法可以自动将boolean的true转换为Integer的1
        User user = BeanUtil.copyProperties(userDto, User.class);

        Boolean result = userService.save(user);
        return Result.success(result);
    }

    // 人员搜索
    @GetMapping("/api/user-service/user/search")
    public Result findByPage(UserDto userDto) {
        PageBean<UserVo> pageBean = userService.findByPage(userDto);
        return Result.success(pageBean);
    }

    //修改人员
    @PutMapping("/api/user-service/user")
    public Result update(@RequestBody UserDto userDto) {
        User user = BeanUtil.copyProperties(userDto, User.class);
        Boolean result = userService.update(user);
        return Result.success(result);
    }

    //删除人员
    @DeleteMapping("/api/user-service/user/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        Boolean result = userService.deleteById(id);
        return Result.success(result);
    }
}
